package com.example.backstage.controller;


import com.example.backstage.service.UserService;
import com.example.gok_mall.constUtil.ConstUtil;
import com.example.gok_mall.entity.User;
import com.example.gok_mall.reponseData.ResponseCode;
import com.example.gok_mall.reponseData.ResultData;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@ResponseBody
@RequestMapping("/manage/user")
@Api(value = "管理员接口类")
public class UserManageController {
    @Autowired
    private UserService userService;

    //管理员登录接口
    @PostMapping("/login.do")
    @ResponseBody
    public ResultData<User> login(String username, String password, HttpSession session) {
        ResultData<User> response = userService.queryUserByUsernameAndPassword(username, password);
        if(response.isSuccess()){
            User user =response.getData();
            if(user.getRole()== ConstUtil.Role.ROLE_ADMIN){
                //说明是管理员
                session.setAttribute(ConstUtil.CURRENT_USER,user);
            }else{
                return ResultData.fail("不是管理员，无法登录");
            }
        }
        //如果登录成功就将管理员信息保存在session中
        if (response.getCode() == ResponseCode.SUCCESS.getCode()) {
            session.setAttribute("userInfo", response.getData());
        }
            return  response;
    }

}
